package com.smartpay.its.framework.remoting.rmi.server.imp;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;

import org.springframework.remoting.rmi.RmiServiceExporter;

import com.smartpay.its.framework.remoting.rmi.server.InRmiServiceManager;

public class RmiServiceManager implements InRmiServiceManager {
	/**
	 * 设置默认Rmi服务端口为1099
	 */
	private int port = 1099;

	public void setPort(int serviceport) {
		port = serviceport;
	}

	/**
	 * 生成单个Rmi服务，参数变量为服务实现的bean,发布服务的名称为接口的simplename
	 */

	public void postService(Object service) throws RemoteException {
		Class<?> name = service.getClass().getInterfaces()[0];
		RmiServiceExporter exporter = new RmiServiceExporter();
		exporter.setServiceName(name.getSimpleName());
		exporter.setServiceInterface(service.getClass().getInterfaces()[0]);
		exporter.setService(service);
		exporter.setRegistryPort(port);
		exporter.afterPropertiesSet();
	}

	/**
	 * 批量生成Rmi服务
	 */

	public void postServices(List<Object> services) throws RemoteException {
		if (null != services && services.size() > 0) {
			Iterator<Object> it = services.iterator();
			while (it.hasNext()) {
				postService(it.next());
			}
		}
	}
}
